<template>
  <div class="container">
    <h1>用户登录</h1>
    <el-form rel="form" label-width="80px" class="login-form">
      <el-form-item label="用户名">
        <el-input type="text" v-model="admin.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="admin.password"></el-input>
      </el-form-item>
      <el-form-item label="验证码">
        <el-input type="text" v-model="admin.captcha"></el-input>
        <el-image style="width: 120px; height: 38px" :src="captchaImageBase64Data" @click="getCaptcha()" />
      </el-form-item>
      <el-form-item label="身份">
        <el-radio-group v-model="admin.user" @change="handleUserRoleChange">
          <el-radio value="管理员" border>管理员</el-radio>
          <el-radio value="用户" border>用户</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
        <el-button type="primary" @click="drawer = true">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
  <!-- 抽屉框 -->
  <el-drawer v-model="drawer" title="用户注册" :with-header="false">
    <el-form :model="adminAdd" :rules="rules" ref="adminForm">  
    <el-form-item label="用户名" label-width="25%" prop="username">  
      <el-input v-model="adminAdd.username" autocomplete="off" />  
    </el-form-item>  
    <el-form-item label="输入密码" label-width="25%" prop="password">  
      <el-input type="password" v-model="adminAdd.password" autocomplete="off" />  
    </el-form-item>  
    <el-form-item label="真实姓名" label-width="25%" prop="name">  
      <el-input v-model="adminAdd.name" autocomplete="off" />  
    </el-form-item>  
    <el-form-item label="网名" label-width="25%" prop="uname">  
      <el-input v-model="adminAdd.uname" autocomplete="off" />  
    </el-form-item>  
    <el-form-item label="性别" label-width="25%" prop="gender">  
      <el-radio-group v-model="adminAdd.gender">
      <el-radio value="男" size="large">男</el-radio>
      <el-radio value="女" size="large">女</el-radio>
    </el-radio-group>
    </el-form-item>  
    <el-form-item label="电话号" label-width="25%" prop="telephone">  
      <el-input v-model="adminAdd.telephone" autocomplete="off" />  
    </el-form-item>  
    <el-form-item label="身份证" label-width="25%" prop="card">  
      <el-input v-model="adminAdd.card" autocomplete="off" />  
    </el-form-item>  
    <div slot="footer" class="dialog-footer">  
      <el-button @click="drawer=false">取消</el-button>  
      <el-button type="primary" @click="submitForm">确认</el-button>  
    </div>  
  </el-form>
  </el-drawer>

</template>

<script setup>
import { ElLoading } from 'element-plus'
import adminApi from '@/api/adminApi';
import router from '@/router';
import { ElMessage } from 'element-plus';
import { ref } from 'vue';
import { useTokenStore } from '@/stores/token';
const TokenStore = useTokenStore();
//表单验证通过
const adminForm = ref(null); 
//抽屉隐藏
const drawer = ref(false)

const admin = ref({
  username: '',
  password: '',
  user: '',
  captchaId: '',
  captcha: ''
});
const captchaImageBase64Data = ref('');


const adminAdd = ref({
  id: '',
  username: '',
  password: '',
  user: '',
  name: '',
  uname: '',
  gender: '男',
  telephone: '',
  card: ''
});

function handleUserRoleChange() {
  sessionStorage.setItem('userRole', admin.value.user);
}
//注册
function insert() {
  //开启加载动画
  const loading = ElLoading.service({
    lock: true,
    text: '加载中',
    background: 'rgba(0, 0, 0, 0.7)',
  })
  adminApi.insert(adminAdd.value)
    .then(resp => {
      loading.close();//取消加载动画
      if (resp.code == 10000) {
        ElMessage({
          message: resp.msg,
          type: 'success',
          duration: 1200
        });
        //隐藏对话框
        drawer.value = false;
        //清空对话框中的数据
        adminAdd.value = {
          id: '',
          username: '',
          password: '',
          user: '',
          name: '',
          uname: '',
          gender: '',
          telephone: '',
          card: ''
        }
      } else {
        ElMessage({
          message: resp.msg,
          type: 'error',
          duration: 2000
        });
      }
    });
}




//登录
function login() {
  sessionStorage.setItem('username', admin.value.username);
  adminApi.lgoin(admin.value)
    .then(resp => {
      if (resp.code == 10000) {
        ElMessage.success({
          message: resp.msg,
          duration: 1200,
          onClose: () => {
            //保存jwt到sessionStorage
            TokenStore.update(resp.data);
            if (sessionStorage.getItem('userRole') == "管理员") {
              router.push('/index');
            } else {
              router.push('/userindex');
            }

          }
        });
      } else {
        ElMessage.error(resp.msg);
      }
    })
}
function getCaptcha() {
  adminApi.captcha()
    .then(resp => {
      admin.value.captchaId = resp.data.captchaId;
      captchaImageBase64Data.value = resp.data.captchaImageBase64Data;
    });
}
//表单校验
const rules = {  
  username: [  
    { required: true, message: '请输入用户名', trigger: 'blur' },  
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }  
  ],  
  password: [  
    { required: true, message: '请输入密码', trigger: 'blur' },  
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }  
  ],  
  name: [  
    { required: true, message: '请输入真实姓名', trigger: 'blur' },  
    { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }  
  ],  
  uname: [  
    { required: true, message: '请输入网名', trigger: 'blur' },  
    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }  
  ],  
  gender: [  
    { required: true, message: '请选择性别', trigger: 'change' }  
  ],  
  telephone: [  
    { required: true, message: '请输入电话号码', trigger: 'blur' },  
    { pattern: /^1[3-9]\d{9}$/, message: '请输入有效的电话号码', trigger: 'blur' }  
  ],  
  card: [  
    { required: true, message: '请输入身份证号码', trigger: 'blur' },  
    { pattern: /^\d{17}[\dXx]$/, message: '请输入有效的身份证号码', trigger: 'blur' }  
  ]  
};  
  
// 提交表单  
function submitForm() {
  adminForm.value.validate((valid) => {  
        if (valid) {  
          console.log('表单验证通过');  
          // 调用你的插入函数或其他逻辑  
          insert();
        } else {  
          console.log('表单不规范');  
          return false;  
        }  
      });  
    };  
    
   


getCaptcha();


</script>
<style scoped>
.container {
  width: 350px;
  border: 1px solid #ccc;
  border-radius: 5px;
  padding: 20px;
  margin: 100px auto;
  background-color: #f9f9f9;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.login-form {
  margin-top: 20px;
}
.el-drawer {
  /* 这里的样式将应用于所有el-drawer组件，但由于scoped属性，它只影响当前组件 */
  /* 例如，你可以改变背景色或边框 */
  background-color: #f9f9f9;
  /* 浅灰色背景 */
  border-radius: 8px;
  /* 圆角边框 */
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  /* 阴影效果 */
}

/* 如果需要更具体的选择器来避免样式冲突，可以使用更深的子选择器 */
.el-drawer .el-form {
  /* 这里设置el-form的样式 */
  margin: 20px;
  /* 增加一些内边距 */
}

.el-drawer .el-form-item {
  /* 这里设置el-form-item的样式 */
  margin-bottom: 15px;
  /* 调整间距 */
}

/* 对话框底部的按钮样式 */
.el-drawer .dialog-footer {
  .footer {
    text-align: right;
    /* 文本右对齐 */
    padding: 10px 20px;
    /* 内边距 */
    border-top: 1px solid #ebeef5;
    /* 上边框 */
  }
}
</style>

